package ddd.leave.domain.rule.repository.persistence;

import ddd.leave.domain.leave.entity.valueobject.LeaveType;
import ddd.leave.domain.person.entity.valueobject.PersonType;
import ddd.leave.domain.rule.entity.ApprovalRule;
import ddd.leave.domain.rule.repository.facade.ApprovalRuleRepositoryInterface;
import ddd.leave.domain.rule.repository.mapper.ApprovalRuleDao;
import ddd.leave.domain.rule.repository.po.ApprovalRulePO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
@Slf4j
public class ApprovalRuleRepositoryImpl implements ApprovalRuleRepositoryInterface {

    @Autowired
    ApprovalRuleDao ruleDao;

    @Override
    public int getLeaderMaxLevel(ApprovalRule rule) {
        String personType = rule.getPersonType();
        String leaveType = rule.getLeaveType();
        if (log.isInfoEnabled()) {
            log.info("personType [{}]  leaveType [{}]   Duration  [{}]  ", personType,leaveType,rule.getDuration());
        }
        ApprovalRulePO rule1 = ruleDao.findRule(personType, LeaveType.valueOf(leaveType), rule.getDuration());
        return rule1.getMaxLeaderLevel();
    }

    @Override
    public int createApprovalRulePO() {
        ApprovalRulePO s = new ApprovalRulePO();
        s.setLeaveType(LeaveType.Internal);
        s.setPersonType(PersonType.INTERNAL);
        s.setApplicantRoleId("234");
        s.setDuration(System.currentTimeMillis());
        ruleDao.save(s);
        return 0;
    }
}
